/*  File Name	: SelectReactor.h
 *  Author		: Javie Pong
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-3-20
 *  Description:
 */

#ifndef SELECTREACTOR_H_
#define SELECTREACTOR_H_
#include "Header.h"
#include "HandleSet.h"
#include "SignalHanlder.h"
#include "ReactorImpl.h"

class EventHandler;
class SignalHanlder;
class TimerHeap;

typedef int (EventHandler::*EH_CallBack)(int);

class SelectReactor:public ReactorImpl
{
public:
	typedef std::map<int,EventHandler*> EventHandleMap;

	SelectReactor();

	virtual ~SelectReactor();

	virtual int open(int handle_size,int timer_heap_size,SignalHanlder *sh=NULL);

	virtual void run_status(int status=0);//reactor run status,1:start 0:stop

	virtual int get_run_status(void);//reactor run status,0:stop 1:start

	virtual int handle_events(int timeout);

	virtual int register_handler(int handle,EventHandler *eh,ReactorMask mask);

	virtual int remove_handler(int handle,EventHandler *eh,ReactorMask mask);

	virtual int register_signal(int signum,EventHandler *eh,bool replace_old,EventHandler *old_sh);

	virtual int remove_signal(int signal,EventHandler *eh);

	virtual long schudle_timer(EventHandler *eh,TimeValue delay,TimeValue interval);

   virtual int suspend_timer( long timer_id);

	virtual int resume_timer( long timer_id);

   virtual int cancel_timer( long timer_id);

	virtual int reactor_type(void);

private:
	int dispatch(int active_handle_num,SelectHandleSet &set);

	int dispatch_io_set(int active_handle_num,int mask,HandleSet &set,EH_CallBack callback);

	int notify_handler(int handle,int mask,EventHandler *eh,EH_CallBack callback);

	int run_status_;

	int max_handle_;

	SelectHandleSet select_handle_set_;

	EventHandleMap event_handle_map_;

	TimerHeap *timer_heap_;

	RT_MUTEX rt_mutex_;
};

#endif /* SELECTREACTOR_H_ */
